package dd.lo.dib;

import dd.lo.HelloCV;
import dd.lo.util.ImageUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class DibFileDisplay extends Application {

    public static void main(String[] args) {
        System.load(HelloCV.class.getResource("/libopencv_java481.dylib").getPath());
        DibFileDisplay.start(args);
    }
    public static void start(String[] args) {
        launch(args);
    }

    private static final int DEFAULT_WIDTH = 1080;
    private static final int DEFAULT_HEIGHT = 768;

    private ImageView imageView;

    private String dibImgPath;



    private void showImg() {
        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() {
                try {
                    FileInputStream fis = new FileInputStream(dibImgPath);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    DibImage dibImage = new DibImage(bis);
                    Mat dst = dibImage.getImageMat();
                    //调整图片大小以用于展示
                    ImageUtils.fitImgSize(dst, DEFAULT_WIDTH, DEFAULT_HEIGHT);
                    MatOfByte buffer = new MatOfByte();
                    Imgcodecs.imencode(".jpeg", dst, buffer);
                    Platform.runLater(() -> {
                        imageView.setImage(new Image(new ByteArrayInputStream(buffer.toArray())));
                    });
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        new Thread(task).start();
    }

    @Override
    public void start(Stage stage) {
        dibImgPath = "/Users/kwok/Downloads/{00234304-C11B-4993-A642-E647BF5C3B62}.dib";
        Button dibImgChooser = new Button("选择dib图片");
        dibImgChooser.setOnMouseClicked(e -> {
            File imgFile = chooseFile(stage);
            if (imgFile == null) return;
            dibImgPath = imgFile.getAbsolutePath();
            showImg();
        });
        FlowPane bottomControl = new FlowPane();
        bottomControl.getChildren().addAll(dibImgChooser);
        bottomControl.setPrefHeight(50);
        imageView = new ImageView();
        BorderPane root = new BorderPane();
        root.setCenter(imageView);
        root.setBottom(bottomControl);
        Scene scene = new Scene(root, DEFAULT_WIDTH, DEFAULT_HEIGHT + 50);
        stage.setScene(scene);
        stage.setTitle("DIB Display");
        stage.setResizable(false);
        stage.show();
    }

    private static File chooseFile(Stage stage) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("请选择图片");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "/Downloads")
        );
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("DIB", "*.dib")
        );
        File pdfFile = fileChooser.showOpenDialog(stage);
        if (null == pdfFile) {
            System.out.println("用户取消选中文件");
            return null;
        }
        return pdfFile;
    }
}
